<template>
  <el-container class="app-container">
    <!--   顶部搜索栏   -->
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-row :gutter="20">
        <el-col :span="14" :push="2">
          <el-form-item label="品类">
            <el-select v-model="queryParams.category" clearable placeholder="品类">
              <el-option v-for="(type, index) in productCategory" :key="index" :label="type" :value="type" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="fetchData(queryParams)">
              <i class="el-icon-zoom-in" />
              <span>查询</span>
            </el-button>
          </el-form-item>
        </el-col>
        <el-col :span="4" :push="2">
          <el-button type="success" round @click="dialogAddFormVisible=true">
            <i class="el-icon-circle-plus-outline" />
            <span>新增商品</span>
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <!--    表格    -->
    <el-main>
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column align="center" label="" width="70">
          <template slot-scope="scope">
            {{ scope.$index }}
          </template>
        </el-table-column>
        <el-table-column label="编号" width="110">
          <template slot-scope="scope">
            {{ scope.row.productId }}
          </template>
        </el-table-column>
        <el-table-column label="品名" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.title }}</span>
          </template>
        </el-table-column>
        <el-table-column label="规格" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.spec }}
          </template>
        </el-table-column>
        <el-table-column label="库存" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.stock }}
          </template>
        </el-table-column>
        <el-table-column label="分类" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.category }}
          </template>
        </el-table-column>
        <el-table-column label="进货价" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.price }}
          </template>
        </el-table-column>
        <el-table-column class-name="status-col" label="售价" width="110" align="center">
          <template slot-scope="scope">
            {{ scope.row.sellPrice }}
          </template>
        </el-table-column>

        <el-table-column align="center" prop="created_at" label="操作" width="300" fixed="right">
          <template slot-scope="scope">
            <el-button type="primary" @click="openEditProduct(scope.$index)">编辑</el-button>
<!--            <el-button type="warning" @click="openEditProduct(scope.$index)">设置预警</el-button>-->
            <el-button type="danger" @click="deleteProduct(scope.row.productId, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="编辑商品信息" :visible.sync="dialogFormVisible" center>
        <el-form :model="productInfoForm">
          <el-form-item label="商品编号" :label-width="formLabelWidth">
            <el-input v-model="productInfoForm.productId" autocomplete="off" disabled style="width: 150px;" />
          </el-form-item>
          <el-form-item label="品名" :label-width="formLabelWidth">
            <el-input v-model="productInfoForm.title" autocomplete="off" />
          </el-form-item>
          <el-form-item label="类别" :label-width="formLabelWidth">
            <el-select v-model="productInfoForm.category" clearable placeholder="品类" size="medium" style="width: 120px;">
              <el-option v-for="(type, index) in productCategory" :key="index" :label="type" :value="type" />
            </el-select>
          </el-form-item>
          <el-form-item label="规格" :label-width="formLabelWidth">
            <el-input v-model="productInfoForm.spec" autocomplete="off" style="width: 100px;" />
          </el-form-item>
          <el-form-item label="库存" :label-width="formLabelWidth">
            <el-input-number v-model="productInfoForm.stock" :min="0" label="库存" size="small" />
          </el-form-item>
          <el-form-item label="进货价" :label-width="formLabelWidth">
            <el-input-number v-model="productInfoForm.price" :precision="2" :step="0.1" :min="0" label="进货价" size="small" />
          </el-form-item>
          <el-form-item label="售价" :label-width="formLabelWidth">
            <el-input-number v-model="productInfoForm.sellPrice" :precision="2" :step="0.1" :min="productInfoForm.price" label="售价" size="small" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="editProduct">确 定</el-button>
        </div>
      </el-dialog>
      <el-dialog title="新增商品" :visible.sync="dialogAddFormVisible" center>
        <el-form :model="productInfoForm">
          <el-form-item label="品名" :label-width="formLabelWidth">
            <el-input v-model="productInfoForm.title" autocomplete="off" />
          </el-form-item>
          <el-form-item label="类别" :label-width="formLabelWidth">
            <el-select v-model="productInfoForm.category" clearable placeholder="品类" size="medium" style="width: 120px;">
              <el-option v-for="(type, index) in productCategory" :key="index" :label="type" :value="type" />
            </el-select>
          </el-form-item>
          <el-form-item label="规格" :label-width="formLabelWidth">
            <el-input v-model="productInfoForm.spec" autocomplete="off" style="width: 100px;" />
          </el-form-item>
          <el-form-item label="库存" :label-width="formLabelWidth">
            <el-input-number v-model="productInfoForm.stock" :min="0" label="库存" size="small" />
          </el-form-item>
          <el-form-item label="进货价" :label-width="formLabelWidth">
            <el-input-number v-model="productInfoForm.price" :precision="2" :step="0.1" :min="0" label="进货价" size="small" />
          </el-form-item>
          <el-form-item label="售价" :label-width="formLabelWidth">
            <el-input-number v-model="productInfoForm.sellPrice" :precision="2" :step="0.1" :min="productInfoForm.price" label="售价" size="small" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addProduct">确 定</el-button>
        </div>
      </el-dialog>
    </el-main>
    <!--    底部分页    -->
    <el-footer>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page="queryParams.current"
        :page-size="queryParams.size"
        style="display: flex;align-items: center;justify-content: center;"
        @current-change="changePage($event)"
      />
    </el-footer>
  </el-container>
</template>

<script>
import { getProductList, getProductCategory, addProduct, deleteProduct, editProduct } from '@/api/product'

export default {
  name: 'ProductList',
  data() {
    return {
      list: null,
      productCategory: [],
      listLoading: true,
      dialogFormVisible: false,
      dialogAddFormVisible: false,
      formLabelWidth: '100px',
      total: 0,
      productInfoForm: {
        index: '',
        productId: '',
        title: '',
        spec: '',
        category: '',
        price: '',
        sellPrice: '',
        stock: ''
      },
      queryParams: {
        category: '',
        size: 6,
        current: 1
      }
    }
  },
  created() {
    this.fetchData(this.queryParams)
    this.getProductCategory()
  },
  methods: {
    fetchData(params) {
      console.log(params)
      this.listLoading = true
      getProductList(params).then(response => {
        const { content } = response
        this.list = content.records
        this.total = content.total
        this.queryParams.current = content.current
        this.queryParams.size = content.size
        this.listLoading = false
      }).catch(error => {
        console.log(error)
      })
    },
    getProductCategory() {
      getProductCategory().then(response => {
        const { content } = response
        this.productCategory = content
      })
    },
    deleteProduct(productId, index) {
      this.$confirm('此操作将永久删除编号为' + productId + '的商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(productId)
        deleteProduct({ productId: productId }).then(response => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.list.splice(index, 1)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    openEditProduct(index) {
      this.dialogFormVisible = true
      this.productInfoForm = this.list[index]
      this.productInfoForm['index'] = index
    },
    editProduct() {
      editProduct(this.productInfoForm).then(response => {
        this.dialogFormVisible = false
        this.list[this.productInfoForm['index']] = this.productInfoForm
        for (const key in this.productInfoForm) {
          this.productInfoForm[key] = ''
        }
      })
    },
    addProduct() {
      addProduct(this.productInfoForm).then(response => {
        console.log(response)
        this.dialogAddFormVisible = false
        const productId = response.content
        this.productInfoForm.productId = productId
        this.list.push(this.productInfoForm)
      })
    },
    changePage(event) {
      // console.log(event)
      this.queryParams.current = event
      this.fetchData(this.queryParams)
    }
  }
}
</script>

